package com.cloud.thelen.wallpaper;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.cloud.thelen.wallpaper.Adapter.HistoryAdapter;
import com.cloud.thelen.wallpaper.Dao.Dao;
import com.cloud.thelen.wallpaper.Model.DaoUtils;
import com.cloud.thelen.wallpaper.Utils.DeleteFile;
import com.cloud.thelen.wallpaper.databinding.ActivityWallpaperBinding;
import com.google.android.material.snackbar.Snackbar;

import java.io.IOException;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private ActivityWallpaperBinding binding;

    private HistoryAdapter adapter;

    private List<Dao> historyBeans;

    private DaoUtils daoUtils;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWallpaperBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.recycler.setOffscreenPageLimit(3);
        daoUtils = new DaoUtils(this);
        historyBeans = daoUtils.queryAll();
        adapter = new HistoryAdapter(this);
        adapter.setView(binding.dialog);
        adapter.setData(historyBeans);
        binding.recycler.setAdapter(adapter);
        binding.download.setText("Delete");
        binding.Cancel.setOnClickListener(l -> {
            onBackPressed();
        });
        binding.dialog.setOnClickListener(l->{
            onBackPressed();
        });
        binding.back.setOnClickListener(l -> {
            onBackPressed();
        });
        binding.title.setOnClickListener(l -> {
            onBackPressed();
        });
        binding.download.setOnClickListener(l -> {
            Dao bean1 = historyBeans.get(binding.recycler.getCurrentItem());
            daoUtils.delete(bean1);
            if (!DeleteFile.delete(bean1.getImg())) {
                Toast.makeText(getApplicationContext(), "Delete the file failure:" + bean1.getImg() + "File not found！", Toast.LENGTH_SHORT).show();
            }
            historyBeans.remove(binding.recycler.getCurrentItem());
            if (historyBeans.size() != 0) {
                adapter.setData(historyBeans);
            } else {
                finish();
            }
            binding.dialog.setVisibility(View.GONE);
        });
        binding.wallpaper.setOnClickListener(l -> {
            Glide.with(getApplicationContext())
                    .asBitmap()
                    .load(historyBeans.get(binding.recycler.getCurrentItem()).getImg())
                    .listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            Snackbar.make(binding.recycler, "Set Fail", Snackbar.LENGTH_SHORT).show();
                            binding.dialog.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                            try {
                                wallpaperManager.suggestDesiredDimensions(1080, 1920);
                                wallpaperManager.setBitmap(resource);
                                Snackbar.make(binding.recycler, "Set Success", Snackbar.LENGTH_SHORT).show();
                                binding.dialog.setVisibility(View.GONE);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return false;
                        }
                    })
                    .preload();
        });
        binding.down.setImageDrawable(getDrawable(R.drawable.ic_baseline_delete_24));
        binding.down.setOnClickListener(l -> {
            daoUtils.deleteAll();
            for (Dao bean : historyBeans) {
                if (!DeleteFile.delete(bean.getImg())) {
                    Toast.makeText(getApplicationContext(), "Delete the file failure:" + bean.getImg() + "File not found！", Toast.LENGTH_SHORT).show();
                }
            }
            finish();
        });
    }

    @Override
    public void onBackPressed() {
        if (binding.dialog.getVisibility() == View.VISIBLE) {
            binding.dialog.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
        return;
    }

}